package org.onion.pattern.sample.active.service;

import com.github.f4b6a3.uuid.UuidCreator;
import org.onion.pattern.event.spring.signal.EventPublished;
import org.onion.pattern.sample.active.service.event.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


@Component
//@EventPublished("none")
public class ActiveService {

    @Autowired
    private ApplicationContext applicationContext;

    @EventPublished
    public String create() {
        applicationContext.publishEvent(new ActCrateEventII());
        return "创建成功";
    }

    public String create2() {
        ActCrateEventExt actCrateEventExt = new ActCrateEventExt();
        actCrateEventExt.setOrderId(UuidCreator.getTimeOrderedEpoch().toString());

        applicationContext.publishEvent(actCrateEventExt);
        return "创建成功";
    }

    @Transactional
    public String update() {
        applicationContext.publishEvent(new ActUpdateEvent());
        return "更新成功";
    }

    public String delete() {
        applicationContext.publishEvent(new ActDeleteEvent());
        return "删除成功";
    }

    @EventPublished("")
    public String flush() {
        applicationContext.publishEvent(new FlushCrateEvent());
        return "flush";
    }


    @Transactional
    public String createFail() {
        applicationContext.publishEvent(new ActCrateEventII());
        if (1 == 1) {
            throw new IllegalStateException("创建失败");
        }
        return "创建成功";
    }


    public String MRocketMQMessaage() {
        applicationContext.publishEvent(new MRocketMQMessaage());
        return "rocketmq 消息发送成功";
    }


    public String MRocketMQMessaageSub() {
        applicationContext.publishEvent(new MRocketMQMessaageSub());
        return "rocketmq 消息发送成功";
    }

}
